﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using Home.Mobilesales.Common;
using Home.MobileSales.Common.Entities;
using Home.MobileSales.Common.Models;
using Home.MobileSales.Print;
using Home.MobileSales.Sales.View;
using OrderSales.Interface;



namespace OrderSales.Presenter
{
    class OrderFormPresenter
    {
        private Order _order;
        private IOrderView _view;
        private BuyPoint _buyPoint;

        public OrderFormPresenter(Order order, BuyPoint buyPoint, IOrderView view)
        {
            _view = view;
            _order = order;
            _buyPoint = buyPoint;
            InitializeView();
        }

        private void InitializeView()
        {
            try
            {
                _view.DateOrder = _order.DateOrder;
                _view.NumberOrder = _order.NumberOrder;
                _view.Amount = _order.SumOrder;
                _view.DataSource = _order.RecordOrders;
                _view.OrderBuyPoint = _buyPoint;
                _view.Sklads = DataManager.GetInstance().Sklads.GetSklads();
                _view.CurrentSklad = _order.SkladId;
                _view.IsVan = _order.NotDelivery;
                _view.AddRecordOrder += AddOrderRecordFormShow;
                _view.RemoveRecordOrder += RemoveRecordOrder;
                _view.ChangeOrder += ChangeOrder;
                _view.PrintOrder += PrintOrder;
            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }

        }

        public void RefreshData()
        {
            try
            {
                _order = DataManager.GetInstance().Orders.GetOrderById(_order.IdOrder);
                _view.DataSource = _order.RecordOrders;
                _view.Amount = _order.SumOrder;
            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }
        }

        public void AddOrderRecordFormShow(object sender, EventArgs e)
        {
            new SelectGoodsForm(_order).ShowDialog();
            RefreshData();
        }

        public void ChangeOrder(object sender, EventArgs e)
        {
            try
            {
                _order.DateOrder = _view.DateOrder;
                _order.NumberOrder = _view.NumberOrder;
                _order.NotDelivery = _view.IsVan;
                if (Settings.GetInstance().IsVanSales && _order.SkladId != ((Sklad)_view.CurrentSklad).IdSklad &&
                    _order.RecordOrders.Count > 0)
                {
                    throw new OtherException("Для зміни складу в VAN режимі потрібно видалити весь товар з накладної!");
                }
                _order.SkladId = ((Sklad)_view.CurrentSklad).IdSklad;
                DataManager.GetInstance().Orders.UpdateOrder(_order);
            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }
        }

        public void RemoveRecordOrder(object sender, EventArgs e)
        {
            try
            {
                OrderRecord orderRec = ((List<OrderRecord>)_view.DataSource)[(int)_view.Current];

                DataManager.GetInstance().OrderRecords.DeleteOrderRecord(
                    orderRec.IdOrderRecord);

                if (Settings.GetInstance().IsVanSales)
                {
                    Remaining rem = DataManager.GetInstance().Remainings.GetRemainingById(_order.SkladId,
                                                                                          orderRec.Good.idGood);
                    rem.Quantity += orderRec.Quantity;
                    DataManager.GetInstance().Remainings.UpdateRemaining(rem);
                }

                RefreshData();

            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }
        }

        public void PrintOrder(object sender, EventArgs e)
        {
            PrintBuilder pb;
            if (_view.IsVan)
                pb = new PrintWayBillTxt();
            else
             pb = new PrintOrderTxt();

            pb.BuyPoint = _buyPoint;
            pb.Order = _order;
            using (pb.writer = new StreamWriter(Settings.GetInstance().PrintPath+"/Print.txt", false, Encoding.Unicode))
            {
                Print print = new Print(pb);
                print.Execute();
            }
            if (Settings.GetInstance().ExecuteProgramPrint != string.Empty)
            {
                Process.Start(new ProcessStartInfo(Settings.GetInstance().ExecuteProgramPrint,""));
            }
        }
    }
}
